# -*- coding:Utf-8 -*-
'''
Created on 27 nov. 2012

@author: fefoley
'''

import datetime

from proofs import Meeting


class TimeStrip(object):
    """
    Represents a period of time, in which one or more tasks were undergone
    self.start : start time of the time strip - typeof(datetime.datetime)
    self.end : end - typeof(datetime.datetime)
    self.kindof : MAIL, MEETING, DEADTIME, ...
    self.flag : description string
    self.evidences : liste of evidences (Mail, Meeting, ...)
    self.label : starting with "start-end : flag", can aggregate several flags
    self.adherence : in minute. number of minutes max by which the timestrip can aggregate to another
    """
    MAIL = 'mail'
    MEETING = 'meeting'
    PARENT = "parent"
    DEADTIME = "deadtime"
    
    def __init__(self, start, end, kindof, flag, evidence, label = "", adherence = 0, children =[]):
        self.start = start
        self.end = end
        self.kindof = kindof
        self.flag = flag
        self.evidences = [evidence]
        prefix = ''   # I want a prefix to the label for meeting 
        if isinstance(evidence, Meeting):
            prefix = '(' + evidence.BusyStatus + ')'
        if label == '': 
            self.label = (prefix + 
                          self.start.strftime("%H:%M") + '-' +
                          self.end.strftime("%H:%M") + ': ' +
                          self.flag)               
        else : 
            self.label = label
        self.adherence = adherence
        self.children = children
    
    @classmethod
    def _fusion(cls, child1, child2):
        """Function to merge two TimeStrip into a new parent TimeStrip linking to them.  """
        start = min(child1.start, child2.start)
        end = max(child1.end, child2.end)
        kindof = cls.PARENT
        flag = cls.PARENT
        evidences = [e for e in child1.evidences] + [e for e in child2.evidences]
        label = child1.label + '\n' + child2.label
        adherence = max(child1.adherence, child2.adherence)        ################## what does this meann ? ###########
        children = [child1, child2]     ############ !!!!!!!!!!!  --> get recursively the deeper childer ?
        return TimeStrip(start, end, kindof, flag, evidences, label, adherence, children)

    @classmethod
    def aggreg(cls, lst, i=0, j=1):
        """
        Aggregation function, that takes a list of TimeStrip and conduct
        aggregation iterations among them. If two TimeStrip are closer in time
        than their adherence variables, they merge, creating a parent TimeStrip that
        takes the children place in the list, and links to them
        return the modified list
        """     
        if i >= len(lst)-1:  # end comparison
            return lst
        if j >=len(lst):  # il est temps d incrementer i
            i = i + 1
            j = i + 1
            return TimeStrip.aggreg(lst, i, j)
        # compare deux elements et eventuellement fusionné
        if lst[j].start - lst[i].end < datetime.timedelta(minutes = max(lst[i].adherence, lst[j].adherence)): #fusion
            lst[i] = cls._fusion(lst[i], lst[j])
            del lst[j]
            return TimeStrip.aggreg(lst, i, j)
        # no fusion, take next j
        return TimeStrip.aggreg(lst, i, j+1)  
        
        
    def __str__(self):
        return self.start.strftime("%H:%M") + '-' + self.end.strftime("%H:%M") +':\n\t' + self.label.replace('\n','\n\t') 
    
    def __repr__(self):
        return self.start.strftime("%H:%M") + '-' + self.end.strftime("%H:%M") +':\n\t' + self.label.replace('\n','\n\t') + '\n'
       
        
        
        